﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfCommCtl.Controls
{
    /// <summary>
    /// LogView2.xaml 的交互逻辑
    /// </summary>

    public partial class LogView2 : UserControl
    {
        ObservableCollection<LogInfo> Logs = new ObservableCollection<LogInfo>();

        /// <summary>
        /// ctor
        /// </summary>
        public LogView2()
        {
            InitializeComponent();
            gdLogs.ItemsSource = this.Logs;
        }

        private static readonly object locker = new object();

        /// <summary>
        /// update log
        /// </summary>
        /// <param name="str"> </param>
        public void LogUpdate(string str)
        {
            if (string.IsNullOrEmpty(str))
                return;
            if (Logs?.Count > 10000)
            {
                Logs?.Clear();
            }
            lock (locker)
            {
                Logs?.Add(new LogInfo(str));
                gdLogs?.ScrollIntoView(Logs[Logs.Count - 1]);
            }
        }

        /// <summary>
        /// 背景颜色
        /// </summary>
        public Brush BackColor
        {
            get { return (Brush)GetValue(BackColorProperty); }
            set { SetValue(BackColorProperty, value); }
        }

        /// <summary>
        /// 背景颜色
        /// </summary>
        public static readonly DependencyProperty BackColorProperty =
            DependencyProperty.Register("BackColorProperty", typeof(Brush), typeof(LogView2), new PropertyMetadata(Brushes.Transparent));

        /// <summary>
        /// 字体颜色
        /// </summary>
        public Brush FontColor
        {
            get { return (Brush)GetValue(FontColorProperty); }
            set { SetValue(FontColorProperty, value); }
        }

        /// <summary>
        /// 字体颜色依赖属性
        /// </summary>
        public static readonly DependencyProperty FontColorProperty =
            DependencyProperty.Register("FontColor", typeof(Brush), typeof(LogView2), new PropertyMetadata(Brushes.White));

        /// <summary>
        /// 错误日志背景颜色
        /// </summary>
        public Brush NGBackColor
        {
            get { return (Brush)GetValue(NGBackColorProperty); }
            set { SetValue(NGBackColorProperty, value); }
        }

        /// <summary>
        /// 错误日志背景颜色依赖属性
        /// </summary>
        public static readonly DependencyProperty NGBackColorProperty =
            DependencyProperty.Register("NGBackColorProperty", typeof(Brush), typeof(LogView2), new PropertyMetadata(Brushes.OrangeRed, new PropertyChangedCallback(ColorPropertyChangedCallback)));

        private static void ColorPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as LogView2).ColorChanged();
        }

        private void ColorChanged()
        {
        }

        /// <summary>
        /// log
        /// </summary>
        public string Log
        {
            get { return (string)GetValue(LogProperty); }
            set { SetValue(LogProperty, value); }
        }

        /// <summary>
        /// log 依赖属性
        /// </summary>
        public static readonly DependencyProperty LogProperty =
            DependencyProperty.Register("Log", typeof(string), typeof(LogView2), new PropertyMetadata(string.Empty, new PropertyChangedCallback(LogPropertyChangedCallback)));

        private static void LogPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as LogView2).LogChanged();
        }

        private void LogChanged()
        {
            LogUpdate(Log);
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            Logs.Clear();
        }
    }
}